home *** CD-ROM | disk | FTP | other *** search
/ Tech Arsenal 1 / Tech Arsenal (Arsenal Computer).ISO / tek-04 / netprog.zip / NETPROG.TAR / lib / sppopen.c < prev    next >
C/C++ Source or Header  |  1989-12-17  |  1KB  |  59 lines

  1. /*
  2.  * Open a SPP connection.
  3.  */
  4.  
  5. #include    "netdefs.h"
  6.  
  7. #include    <netns/ns.h>
  8.  
  9. /*
  10.  * The following globals are available to the caller, if desired.
  11.  */
  12.  
  13. struct sockaddr_ns    spp_srv_addr;    /* server's XNS socket addr */
  14.  
  15. int            /* return socket descriptor if OK, else -1 on error */
  16. spp_open(host, service, port)
  17. char    *host;        /* name of other system to communicate with */
  18.                    /* must be acceptable to ns_addr(3N) */
  19.                    /* <netid><separator><hostid><separator><port> */
  20. char    *service;    /* name of service being requested */
  21.                 /* not currently used */
  22. int    port;        /* if < 0, bind a local reserved port (TODO) */
  23.             /* if > 0, it's the port# of server */
  24. {
  25.     int        fd;
  26.     struct ns_addr    ns_addr();    /* BSD library routine */
  27.  
  28.     if ( (fd = socket(AF_NS, SOCK_STREAM, 0)) < 0) {
  29.         err_ret("spp_open: can't create SPP stream socket");
  30.         return(-1);
  31.     }
  32.  
  33.     /*
  34.      * Set up the server's address.
  35.      */
  36.  
  37.     bzero((char *) &spp_srv_addr, sizeof(spp_srv_addr));
  38.     spp_srv_addr.sns_family = AF_NS;
  39.     spp_srv_addr.sns_addr   = ns_addr(host);
  40.                     /* stores net-ID, host-ID and port */
  41.     if (port > 0)
  42.         spp_srv_addr.sns_addr.x_port = htons(port);
  43.     else if (port < 0)
  44.         err_quit("spp_open: reserved ports not implemeneted yet");
  45.  
  46.     /*
  47.      * And connect to the server.
  48.      */
  49.  
  50.     if (connect(fd, (struct sockaddr *) &spp_srv_addr,
  51.                         sizeof(spp_srv_addr)) < 0) {
  52.         err_ret("spp_open: can't connect to server");
  53.         close(fd);
  54.         return(-1);
  55.     }
  56.  
  57.     return(fd);    /* all OK */
  58. }
  59.